﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.Systems
{
	public partial class UsersForm : ListAndViewBaseForm
	{
		public UsersForm()
		{
			InitializeComponent();

			ucFormInput1.InitializeControls(typeof(Models.User));

			InitializeSpecial();
		}
		

		public UsersForm(bool select)
		{
			InitializeComponent();

			ucFormInput1.InitializeControls(typeof(Models.User));

			isSelect = select;

			InitializeSpecial();
		}

		private void InitializeSpecial()
		{
			(ucFormInput1.GetControlByName("PassWord") as TextBox).PasswordChar = '⊙';
			Label lb = new Label();
			lb.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
			lb.TextAlign = ContentAlignment.MiddleRight;
			lb.Text = "确认密码";
			ucFormInput1.tabPanel.Controls.Add(lb, 0, 3);

			TextBox txt = new TextBox();
			txt.Name = "ConfirmPwd";
			txt.PasswordChar = '⊙';
			txt.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
			txt.Margin = new Padding(0, 0, 20, 0);
			ucFormInput1.tabPanel.Controls.Add(txt, 1, 3);
		}

		private void UsersForm_Load(object sender, EventArgs e)
		{
			try
			{
				BindData();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					this.panel2.Enabled = true;
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void lvData_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					this.panel2.Enabled = false;
					this.state = State.Edit;

					CarDBContext db = new CarDBContext();
					int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
					User user = db.Users.FirstOrDefault(p => p.UserID == id);
					if (user != null)
					{
						ucFormInput1.SetValues(user);
						ucFormInput1.GetControlByName("ConfirmPwd").Text = user.PassWord;
					}

				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}

		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					if (MessageBox.Show(this, "是否删除当前零件类型？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						CarDBContext db = new CarDBContext();

						int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
						db.Users.Remove(db.Users.First(p => p.UserID == id));

						db.SaveChanges();

						BindData();
						ucFormInput1.ClearData();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				if (!ucFormInput1.VerifyInput())
				{
					return;
				}

				CarDBContext db = new CarDBContext();

				if (this.state == State.Edit)
				{
					int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
					User user = db.Users.First(p => p.UserID == id);

					ucFormInput1.GetValues(user);

					db.SaveChanges();
					BindData();
					this.panel2.Enabled = false;

					this.ShowInfo("保存成功！");
				}
				else
				{
					//查询用户编码是否重复
					string strCode = ucFormInput1.GetControlByName("UserCode").Text;
					if (db.Users.SingleOrDefault(p => p.UserCode == strCode) != null)
					{
						this.ShowInfo("用户编码已经存在");
						//tabPanel.Controls["Code"]();
						ucFormInput1.GetControlByName("UserCode").Focus();
						return;
					}

					var user = db.Users.Create();
					ucFormInput1.GetValues(user);

					db.Users.Add(user);
					db.SaveChanges();
					BindData();

					if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						this.panel2.Enabled = true;
						this.state = State.Add;
						ucFormInput1.ClearData();

						ucFormInput1.GetControlByName("UserCode").Focus();
					}
					else
					{
						this.panel2.Enabled = false;
						this.state = State.Add;
						ucFormInput1.ClearData();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void lvData_DoubleClick(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					if (isSelect)
					{
						//CarDBContext db = new CarDBContext();

						//int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
						//goodType = db.GoodType.First(p => p.GoodTypeID == id);

						//this.DialogResult = System.Windows.Forms.DialogResult.OK;
					}
					else
					{
						btnEdit_Click(sender, e);
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		private void BindData()
		{
			try
			{
				lvData.Items.Clear();

				CarDBContext db = new CarDBContext();
				var list = db.Users.Where(p => p.UserCode != "admin");

				foreach (var item in list)
				{
					ListViewItem lvItem = new ListViewItem(new string[] { item.UserCode, item.UserName, item.IsEnable, item.Description });
					lvItem.Tag = item.UserID;
					lvData.Items.Add(lvItem);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

	}
}
